package com.ccl.service.impl;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ccl.utils.SmsUtils;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class SmsServiceImpl {
    public String sendSms(String tel) throws Exception {
        //获取客户端
        com.aliyun.dysmsapi20170525.Client client = SmsUtils.createClient("LTAI5t8iBDpKTDPgBu2TemAm", "ftvLsrdiwMuCrRKVm6xAfzanUbJYRJ");
        //准备短信验证码
        String code = "";
        Random random = new Random();
        for (int i=0;i<4;i++){
            int num = random.nextInt(10);
            code += num;
        }
        System.out.println("code="+code);
        //创建发送短信的请求
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("阿里云短信测试")  //指定签名
                .setTemplateCode("SMS_154950909") //短信模板
                .setPhoneNumbers(tel)
                .setTemplateParam("{\"code\":\""+code+"\"}");
        // 复制代码运行请自行打印 API 的返回值
        client.sendSms(sendSmsRequest);
        return code;
    }
}
